package com.demo.java.OD201_250.OD231;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 */
public class Trie {
    private TrieNode root;

    public Trie() {
        root = new TrieNode();
    }

    // 向Trie中插入密码
    public void insert(String word) {
        TrieNode current = root;
        for (char c : word.toCharArray()) {
            current.children.putIfAbsent(c, new TrieNode());
            current = current.children.get(c);
        }
        current.isEndOfWord = true;
    }

    // 判断Trie中是否存在某个前缀
    public boolean searchPrefix(String word) {
        TrieNode current = root;
        for (char c : word.toCharArray()) {
            if (!current.children.containsKey(c)) {
                return false;
            }
            current = current.children.get(c);
        }
        return true; // 前缀存在
    }
}
